package com.ztmg.water.service.system.impl;

import com.ztmg.common.util.UUIDUtils;
import com.ztmg.water.model.system.SysUser;
import com.ztmg.water.vo.common.CommonFlag;
import com.ztmg.water.vo.system.SysUserVO;
import com.ztmg.water.dto.system.SysUserDTO;
import com.ztmg.water.dto.system.SysUserPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.system.SysUserService;
import com.ztmg.water.mapper.system.SysUserMapper;import com.ztmg.water.model.system.SysUserExample;

@Service
public class SysUserServiceImpl implements SysUserService{

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public JsonResult<SysUserVO> loginValidate(SysUserDTO dto) {
        if(null != dto){
            SysUserExample example = new SysUserExample();
            SysUserExample.Criteria criteria =  example.createCriteria();
            criteria.andLoginNameEqualTo(dto.getLoginName());
            criteria.andUserPwdEqualTo(dto.getUserPwd());
            SysUserVO vo = new SysUserVO();
            List<SysUser> beanList = sysUserMapper.selectByExample(example);
            if(!beanList.isEmpty()){
                BeanUtils.copyProperties(beanList.get(0),vo);
            }else
            {
                return JsonResult.fail(ErrorConst.SYSTEM_USER_LOGIN_FAILTURE,ErrorConst.SYSTEM_USER_LOGIN_FAILTURE_MSG);
            }
            return JsonResult.success(vo);
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult create(SysUserDTO dto) {
        if(null != dto){
            SysUser bean = new SysUser();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            bean.setStatus(1);
            bean.setUserId(UUIDUtils.generateShortUuid());
            bean.setUserPwd("123456");
            bean.setUserType(1);
            //后续完善
            bean.setCreater("");
            sysUserMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(SysUserDTO dto) {
        if(null != dto && null != dto.getId()){
            SysUser bean = new SysUser();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            sysUserMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<SysUserVO> selectById(Integer id) {
        if(null != id){
            SysUser bean = sysUserMapper.selectByPrimaryKey(id);
            SysUserVO vo = new SysUserVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<SysUserVO> select(SysUserDTO dto) {
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<SysUser> sysUserList = sysUserMapper.selectByExample(example);
        SysUserVO vo = new SysUserVO();
        if(!sysUserList.isEmpty()){
            BeanUtils.copyProperties(sysUserList.get(0),vo);
        }
        return JsonResult.success(vo);
    }

    @Override
    public JsonResult<PageInfo<SysUserVO>> selectPage(SysUserPageDTO dto) {
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<SysUser> beanList = sysUserMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<SysUserVO> respDTOS = beanList.parallelStream().map((bean) -> {
            SysUserVO vo = new SysUserVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( sysUserMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(SysUserDTO dto) {
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(sysUserMapper.deleteByExample(example));
    }

    @Override
    public JsonResult batchDelete(List<Integer> ids) {
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria =  example.createCriteria();
        criteria.andIdIn(ids);
        return JsonResult.success(sysUserMapper.deleteByExample(example));
    }
}